#include <limits>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;

class Complex{
public:
    Complex(double real,double image)
        : _real(real)
          , _image(image)
    {
        cout << "Complex(double,double)" << endl;
    }


    void print() const{
        cout << _real << " + " << _image << "i" << endl;
    }
    friend istream & operator>>(istream & is,Complex & rhs);
    friend ostream & operator<<(ostream & os,const Complex & rhs);
private:
    double _real;
    double _image;
};

void readDoubleNumber(istream & is,double & number){
    while(is >> number,!is.eof()){
        if(is.bad()){
            cout << "istream has broken!" << endl;
            return;
        }else if(is.fail()){
            is.clear();
            is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            cout << "please input a double number" << endl;
        }else{
            break;
        }
    }
}

ostream & operator<<(ostream & os,const Complex & rhs){
    os << rhs._real << "+" << rhs._image << "i";
    return os;
}

istream & operator>>(istream & is,Complex & rhs){
    cout << "please input a real:" << endl;
    /* cin >> rhs._real; */
    readDoubleNumber(is,rhs._real);
    cout << "please input an image:" << endl;
    /* cin >> rhs._image; */
    readDoubleNumber(is,rhs._image);
    return is;
}

/* istream & operator>>(istream & is,Complex & rhs){ */
/*     cin >> rhs._real >> rhs._image; */
/*     return is; */
/* } */
void test0(){
    Complex c1(1,2);
    c1.print();
    cout << c1 << endl;
    cin >> c1;
    cout << "c1:" << c1 << endl;
}

int main(void){
    test0();
    return 0;
}
